using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for bzPagination
/// </summary>
public class bzPagination
{    
    private int _Limit = 10;
    private int _PageNumber = 1; 
    private string _Next = "Next";
    private string _Prev = "Previous";
    private string _First = "First";
    private string _Last = "Last";
    private long _Total = 0;
    private string _Page = "Page";
    private string _URL = "";   
    private int _ItemShowNumber = 5;

    public string URL
    {
        set { _URL = value; }
    }
    public int ItemShowNumber
    {
        set { _ItemShowNumber = value; }
    }
    public string Page
    {
        set { _Page = value; }
    }
    public long Total
    {
        set { _Total = value; }
    }
    public string First
    {
        set { _First = value; }
    }
    public string Last
    {
        set { _Last = value; }
    }
    public int Limit
    {
        set { _Limit = value; }        
    }
    
    public int PageNumber
    {
        set { _PageNumber = value; }        
    }
   
    public string Next
    {
        set { _Next = value; }        
    }
    
    public string Previous
    {
        set { _Prev = value; }        
    }    
	/// <summary>
	/// Return string for pagination 
	/// </summary>
	/// <param name="sPage">String to show "Page"</param>
	/// <param name="iPageNumber">Current page</param>
	/// <param name="iLimit">Limit item to dislay</param>
	/// <param name="lTotal">Total of record</param>
	/// <param name="iItemShowNuber">Number of page to show</param>
	/// <param name="sNext">String "Next"</param>
	/// <param name="sPrev">String "Previous"</param>
	/// <param name="sFirst">String "First page"</param>
	/// <param name="sLast">String "Last page"</param>
	/// <param name="sURL">URL to Link</param>    
    public bzPagination(string sPage,int iPageNumber,int iLimit,long lTotal,int iItemShowNuber,string sNext,string sPrev,string sFirst,string sLast,string sURL)
    {
        _Page = sPage;
        _PageNumber = iPageNumber;
        _Limit = iLimit;
        _Total = lTotal;
        _ItemShowNumber = iItemShowNuber;
        _Next = sNext;
        _Prev = sPrev;
        _First = sFirst;
        _Last = sLast;
        _URL = sURL;
    }
    public bzPagination()
    { 
    }
    public string getStringPagination()
    {
        if (_Limit >= _Total) return String.Empty;
        long tmp = _Total / _Limit;
        int item=_ItemShowNumber/2;
        string strTmp ="<div id=\"bzPagination\"><strong>"+ _Page+"</strong>&nbsp;";
        if(!_URL.Contains("?")) _URL+="?";
        if (_PageNumber > 1)
        {
            strTmp += "<a href=\"" + _URL + "&Page=1\">" + _First + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL + "&Page="+(_PageNumber-1)+"\">" + _Prev + "</a>&nbsp;";  
        }

        for (int i = 0; i < tmp; i++)
        {
            if (i+1 == _PageNumber) strTmp += "<span id=\"bzPaginationActive\">" + (i+1) + "</span>&nbsp;";
            else if(((i+1>=_PageNumber-item) && (i+1<=_PageNumber+item)) ||((i+1>_Total-_ItemShowNumber)&&(i+1<_PageNumber)) ||((i+1<_ItemShowNumber)&&(i+1>_PageNumber)) )strTmp += "<a href=\"" + _URL + "&Page="+(i+1)+"\">"+(i+1)+"</a>&nbsp;";
        }
        if (tmp * _Limit < _Total)
        {
            if (_PageNumber >= tmp+1) strTmp += "<span id=\"bzPaginationActive\">" + (tmp + 1) + "</span>&nbsp;";
            else             
            {
                strTmp+="<a href=\"" + _URL + "&Page=" + (tmp + 1) + "\">" + (tmp + 1) + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL + "&Page=" + (_PageNumber + 1) + "\">" + _Next + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL + "&Page=" + (tmp + 1) + "\">" + _Last + "</a>&nbsp;";                
            }
        }
        else if (_PageNumber < tmp )
        {
            strTmp += "<a href=\"" + _URL + "&Page=" + (_PageNumber + 1) + "\">" + _Next + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL + "&Page=" + (tmp ) + "\">" + _Last + "</a>&nbsp;";            
        }
        return strTmp+"</div>";
    }
    public string getStringPagination(object exp)
    {
        if (_Limit >= _Total) return String.Empty;
        long tmp = _Total / _Limit;
        int item = _ItemShowNumber / 2;
        string strTmp = "<div id=\"bzPagination\"><strong>" + _Page + "</strong>&nbsp;";
        if (_PageNumber > 1)
        {
            strTmp += "<a href=\"" + _URL +  "1" + exp + "\">" + _First + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL + (_PageNumber - 1) + exp + "\">" + _Prev + "</a>&nbsp;";
        }

        for (int i = 0; i < tmp; i++)
        {
            if (i + 1 == _PageNumber) strTmp += "<span id=\"bzPaginationActive\">" + (i + 1) + "</span>&nbsp;";
            else if (((i + 1 >= _PageNumber - item) && (i + 1 <= _PageNumber + item)) || ((i + 1 > _Total - _ItemShowNumber) && (i + 1 < _PageNumber)) || ((i + 1 < _ItemShowNumber) && (i + 1 > _PageNumber))) strTmp += "<a href=\"" + _URL +  (i + 1) + exp + "\">" + (i + 1) + "</a>&nbsp;";
        }
        if (tmp * _Limit < _Total)
        {
            if (_PageNumber >= tmp + 1) strTmp += "<span id=\"bzPaginationActive\">" + (tmp + 1) + "</span>&nbsp;";
            else
            {
                strTmp += "<a href=\"" + _URL +  (tmp + 1) + exp + "\">" + (tmp + 1) + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL +  (_PageNumber + 1) + exp + "\">" + _Next + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL + (tmp + 1) + exp + "\">" + _Last + "</a>&nbsp;";
            }
        }
        else if (_PageNumber < tmp)
        {
            strTmp += "<a href=\"" + _URL +  (_PageNumber + 1) + exp + "\">" + _Next + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL +  (tmp) + exp + "\">" + _Last + "</a>&nbsp;";
        }
        return strTmp + "</div>";
    }
    public string getStringPagination(object chr, object exp)
    {
        if (_Limit >= _Total) return String.Empty;
        long tmp = _Total / _Limit;
        int item = _ItemShowNumber / 2;
        string strTmp = "<div id=\"bzPagination\"><strong>" + _Page + "</strong>&nbsp;";
        if (_PageNumber > 1)
        {
            strTmp += "<a href=\"" + _URL +chr+ "1" + exp + "\">" + _First + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL + chr + (_PageNumber - 1) + exp + "\">" + _Prev + "</a>&nbsp;";
        }

        for (int i = 0; i < tmp; i++)
        {
            if (i + 1 == _PageNumber) strTmp += "<span id=\"bzPaginationActive\">" + (i + 1) + "</span>&nbsp;";
            else if (((i + 1 >= _PageNumber - item) && (i + 1 <= _PageNumber + item)) || ((i + 1 > _Total - _ItemShowNumber) && (i + 1 < _PageNumber)) || ((i + 1 < _ItemShowNumber) && (i + 1 > _PageNumber))) strTmp += "<a href=\"" + _URL + chr + (i + 1) + exp + "\">" + (i + 1) + "</a>&nbsp;";
        }
        if (tmp * _Limit < _Total)
        {
            if (_PageNumber >= tmp + 1) strTmp += "<span id=\"bzPaginationActive\">" + (tmp + 1) + "</span>&nbsp;";
            else
            {
                strTmp += "<a href=\"" + _URL + chr + (tmp + 1) + exp + "\">" + (tmp + 1) + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL + chr + (_PageNumber + 1) + exp + "\">" + _Next + "</a>&nbsp;";
                strTmp += "<a href=\"" + _URL + chr + (tmp + 1) + exp + "\">" + _Last + "</a>&nbsp;";
            }
        }
        else if (_PageNumber < tmp)
        {
            strTmp += "<a href=\"" + _URL + chr + (_PageNumber + 1) + exp + "\">" + _Next + "</a>&nbsp;";
            strTmp += "<a href=\"" + _URL + chr + (tmp) + exp + "\">" + _Last + "</a>&nbsp;";
        }
        return strTmp + "</div>";
    }
    public string getStringPaginationAjax(int iAction)
    {
        if (_Limit >= _Total) return String.Empty;
        long tmp = _Total / _Limit;
        int item = _ItemShowNumber / 2;
        string strTmp = "<div id=\"bzPagination\"><strong>" + _Page + "</strong>&nbsp;";
        if (!_URL.Contains("?")) _URL += "?";
        if (_PageNumber > 1)
        {
            strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=1',"+iAction+");return false;\">" + _First + "</a>&nbsp;";
            strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (_PageNumber - 1) + "',"+iAction+");return false;\">" + _Prev + "</a>&nbsp;";
        }

        for (int i = 0; i < tmp; i++)
        {
            if (i + 1 == _PageNumber) strTmp += "<span id=\"bzPaginationActive\">" + (i + 1) + "</span>&nbsp;";
            else if (((i + 1 >= _PageNumber - item) && (i + 1 <= _PageNumber + item)) || ((i + 1 > _Total - _ItemShowNumber) && (i + 1 < _PageNumber)) || ((i + 1 < _ItemShowNumber) && (i + 1 > _PageNumber))) strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (i + 1) + "',"+iAction+");return false;\">" + (i + 1) + "</a>&nbsp;";
        }
        if (tmp * _Limit < _Total)
        {
            if (_PageNumber >= tmp + 1) strTmp += "<span id=\"bzPaginationActive\">" + (tmp + 1) + "</span>&nbsp;";
            else
            {
                strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (tmp + 1) + "'," + iAction + ");return false;\">" + (tmp + 1) + "</a>&nbsp;";
                strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (_PageNumber + 1) + "',"+iAction+");return false;\">" + _Next + "</a>&nbsp;";
                strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (tmp + 1) + "',"+iAction+");return false;\">" + _Last + "</a>&nbsp;";
            }
        }
        else if (_PageNumber < tmp)
        {
            strTmp += "<a href=\"#\" onclick=\"createAjax('" + _URL + "&Page=" + (_PageNumber + 1) + "',"+iAction+");return false;\">" + _Next + "</a>&nbsp;";
            strTmp += "<a href=\"#\" onclick=\"createAjax('"+ _URL + "&Page=" + (tmp) + "',"+iAction+");return false;\">" + _Last + "</a>&nbsp;";
        }
        return strTmp + "</div>";
    }    
}
